--[[
Nog-A-Holic
]]

--[[			=Locals=			]]
local nogg = {["skel"] = "Interface\\Icons\\Spell_Shadow_RaiseDead",
					["slow"] = "Interface\\Icons\\Spell_Magic_FeatherFall",
					["mini"] = "Interface\\Icons\\Spell_Shadow_GrimWard",
					["Pot"] = "Interface\\Icons\\INV_Potion_83",
					}
local noggtype = nil
local d = 0

--[[
Localisation
Please post these on googlecode, http://wow-zariel.googlecode.com]]
local L = GetLocale() == "deDE" and {
	["Tanaris"] = "Tanaris"
} or {
	["Tanaris"] = "Tanaris"
}
--[[			=Init=				]]
local Noggaholic = DongleStub("Dongle-1.1"):New("Noggaholic")

--[[			=db INIT=		]]
local databasedefaults = {
	profile = {	["needed"] = 80,
		},
}

function Noggaholic:Initialize()
	self:EnableDebug(d)
	self.db = self:InitializeDB("NoggaholicDB", databasedefaults)
	self.slashcmd = self:InitializeSlashCommand("Nogg-a-Holic","NOGGAHOLIC","nogg")
end

function Noggaholic:Enable()
	self:RegisterEvent("UNIT_AURA")
	self:RegisterEvent("MERCHANT_SHOW")
	self:RegisterEvent("BAG_UPDATE")
	self.slashcmd:RegisterSlashHandler("Needed: Max amount to buy when visiting Marin", "^needed (.+)$'", "SetNeed")
	self:UNIT_AURA(nil, "player")
end

function Noggaholic:Disable()
	self:Print("Disabled")
	self:UnregisterAllEvents()
	f = nil
end

--[[Frames]]
local f = CreateFrame("Button", "NoggFrame", UIParent, "SecureActionButtonTemplate")

f:SetAttribute("type1", "item")			
f:SetAttribute("item1", "Noggenfogger Elixir")

f:SetFrameStrata("BACKGROUND")
f:SetWidth(45)
f:SetHeight(45)

f:EnableMouse(true)
f:RegisterForClicks("AnyUp")

f:RegisterForDrag("RightButton")
f:SetMovable(true)
f:SetClampedToScreen(true)

f:SetScript("OnDragStart", function(f)
	f:StartMoving()
end)

f:SetScript("OnDragStop", function(f)
	f:StopMovingOrSizing()
end)



local t = f:CreateTexture(nil,"BACKGROUND")
t:SetTexture(noggtype)
t:SetAllPoints(f)
f.texture = t

local font1 = f:CreateFontString(nil,"Overlay")
font1:SetFont(STANDARD_TEXT_FONT, 16)
font1:SetTextColor(255,255,255,1)
font1:SetJustifyH("LEFT")
font1:SetShadowOffset(.8, -.8)
font1:SetShadowColor(0, 0, 0, 1)
font1:ClearAllPoints()
font1:SetPoint("TOP", f, 0, 0)
font1:SetText(GetItemCount("Noggenfogger Elixir"))

local font2 = f:CreateFontString(nil,"Overlay")
font2:SetFont(STANDARD_TEXT_FONT, 14)
font2:SetTextColor(255,255,255,1)
font2:SetJustifyH("LEFT")
font2:SetShadowOffset(.8, -.8)
font2:SetShadowColor(0, 0, 0, 1)
font2:ClearAllPoints()
font2:SetPoint("BOTTOMRIGHT", f, 5, 0)

f:SetPoint("CENTER",0,0)

f:SetScript("OnUpdate", function(self)
	for i = 0,16 do
		local BuffName = GetPlayerBuffName(i)
		if BuffName == "Noggenfogger Elixir" then
			local timeLeft = GetPlayerBuffTimeLeft(i)
			if timeLeft > 0 then
				local timeLeft = Noggaholic:BuffTime(GetPlayerBuffTimeLeft(i))
				font2:SetText(timeLeft)
			else
				font2:SetText("")
			end
		break
		else
			font2:SetText("")
		end
	end
end)

function Noggaholic:BAG_UPDATE()
font1:SetText(GetItemCount("Noggenfogger Elixir"))
end

--[[From SeriousBuffTimers, seriously]]
function Noggaholic:BuffTime(num)
	local m = math.floor(num / 60)
	local s = math.fmod(num, 60)
	return string.format("%d:%02d", m, s)
end

--[[Finds what nogg we have]]
function Noggaholic:UNIT_AURA(event, arg1)
font1:SetText(GetItemCount("Noggenfogger Elixir"))
	if arg1 == "player" then
		for i = 1,24 do
			local name, _, texture, _, duration, timeleft =  UnitBuff("player", i)
			if texture then
			local texture = tostring(texture)
				if name == "Noggenfogger Elixir" then
				self:Print(tostring(texture))
					if texture == nogg["skel"] then
						noggtype = nogg["skel"]
						t:SetTexture(noggtype)
						break
					elseif texture == nogg["slow"] then
						noggtype = nogg["slow"]
						t:SetTexture(noggtype)
						f:Show()
						self:Debug(1, "Slow fall", noggtype)
						break
					elseif texture == nogg["mini"] then
						noggtype = nogg["mini"]
						t:SetTexture(noggtype)
						f:Show()
						self:Debug(1, "Small man",  noggtype)
						break
					end
				else
					t:SetTexture(nogg["Pot"])
				end
			else
				return
			end
		end
	end
end

--[[Some code to buy and sort noggs at Marin]]
function Noggaholic:MERCHANT_SHOW()
	local zone = GetZoneText()
	self:Debug(1, zone, L[zone])
	if zone == L["Tanaris"] then
	local name = GetMerchantItemInfo(1)
		if name == "Noggenfogger Elixir" then
		self:Debug(1, "This guy sells noggenfogger", L["Tanaris"])
			for bags=0,4 do
				for slot=0,GetContainerNumSlots(bags) do
				local link = GetContainerItemLink(bags, slot)
					if link then
					local iName = self:LinkToStr(link)
						if iName == "Noggenfogger Elixir" then
						local current = GetItemCount(link)
						local needed = math.floor((self.db.profile["needed"] - current)/5)
						self:Debug(1, needed.." "..current)
							if needed > 0 then
							BuyMerchantItem(1, needed)
							font1:SetText(GetItemCount("Noggenfogger Elixir"))
							end
						break
						end
					end
				end
			end
		else
			return
		end
	end
end

--[[Makes itemnames]]
function Noggaholic:LinkToStr(itemlink)
	return strtrim(select(1, (GetItemInfo(string.match(itemlink, "item:(%d+)")))))
end

function Noggaholic:SetNeed(num)
	if num ~= "" then
		self.db.profile["needed"] = num
	else
		self:Print("Value needs to be a number")
	end
end
